package com.atguigu.guli.serviceedu.controller;


import com.atguigu.guli.serviceedu.entity.Course;
import com.atguigu.guli.serviceedu.entity.query.CourseQuery;
import com.atguigu.guli.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.guli.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.guli.serviceedu.service.ChapterService;
import com.atguigu.guli.serviceedu.service.CourseService;
import com.atguigu.guli.serviceedu.service.VideoService;
import com.atguigu.servicebase.exception.Assert;
import com.atguigu.servicebase.result.R;
import com.atguigu.servicebase.result.ResponseEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-01
 */

@Api(tags = "课程管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/edu/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private VideoService videoService;

    @CacheEvict(value = "course-index", allEntries=true)
    @ApiOperation(value = "新增课程")
    @PostMapping("save")
    public R saveCourse(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo){
        String courseId = courseService.saveCourse(courseInfoVo);
        return R.ok().data("courseId", courseId);
    }

    //根据课程id查询课程基本信息
    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("course-info/{courseId}")
    public R getCourseInfo(@PathVariable Long courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfoById(courseId);
        return R.ok().data("item",courseInfoVo);
    }

    @ApiOperation(value = "根据id更新课程")
    @PutMapping("update-course-info")
    public R updateCourseInfoById(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfoById(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("course-publish-info/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true, example = "1")
            @PathVariable String id){
        CoursePublishVo courseInfoForm = courseService.getCoursePublishVoById(id);
        return R.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true, example = "1")
            @PathVariable String id){
        courseService.publishCourseById(id);
        return R.ok().message("发布成功");
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true, example = "1")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true, example = "8")
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            CourseQuery courseQuery){
        Page<Course> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<Course> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  R.ok().data("total", total).data("rows", records);
    }

    @CacheEvict(value = "course-index", allEntries=true)
    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true, example = "1")
            @PathVariable String id){
        boolean result = courseService.removeById(id);
        Assert.notFalse(result, ResponseEnum.DELETE_COURSE_ERROR);
        chapterService.removeByCourseId(id);
        videoService.removeByCourseId(id);
        return R.ok().message("删除成功");
    }

}

